<template>
  <a-modal
    :title="title"
    :width="1000"
    :visible="visible"
    :okButtonProps="{ props: {disabled: disableSubmit} }"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <!-- 主表单区域 -->
      <a-form :form="form">
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="姓名">
          <a-input placeholder="请输入姓名" v-decorator="['name', {rules: [{ required: true, message: '请输入姓名' }]}]"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="性别">
          <!--<a-input placeholder="请输入性别" v-decorator="['sex', {}]" />-->
          <j-dict-select-tag v-decorator="['sex', {rules: [{ required: true, message: '请选择性别' }]}]" :triggerChange="true" placeholder="请选择"
                             dictCode="sex"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="名单来源">
          <!--<a-input placeholder="请输入性别" v-decorator="['sex', {}]" />-->
          <a-input v-decorator="['customerFrom', {}]" placeholder="请输入"
                             />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="老师姓名">
          <!--<a-input placeholder="请输入性别" v-decorator="['sex', {}]" />-->
          <a-input  v-decorator="['teacherName', {}]"  placeholder="请输入"
                             />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="手机号码">
          <a-input placeholder="请输入手机号码" v-decorator="['phone', {}]"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="微信">
          <a-input placeholder="请输入微信" v-decorator="['wechat', {}]"/>
        </a-form-item>
        <a-form-item
        :labelCol="labelCol"
        :wrapperCol="wrapperCol"
        label="学历">
        <!--<a-input placeholder="请输入学历" v-decorator="['edu', {}]" />-->
        <j-dict-select-tag v-decorator="['edu', {}]" :triggerChange="true" placeholder="请选择"
        dictCode="xc_edu"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="意向级别">
          <!--<a-input placeholder="请输入意向级别" v-decorator="['likeLevel', {}]" />-->
          <j-dict-select-tag v-decorator="['likeLevel', {}]" :triggerChange="true" placeholder="请选择"
                             dictCode="xc_like_level"/>
        </a-form-item>
        <!--<a-form-item-->
        <!--:labelCol="labelCol"-->
        <!--:wrapperCol="wrapperCol"-->
        <!--label="是否报名">-->
        <!--&lt;!&ndash;<a-input placeholder="请输入是否报名" v-decorator="['isApplyed', {}]" />&ndash;&gt;-->
        <!--<j-dict-select-tag v-decorator="['isApplyed', {}]" :triggerChange="true" placeholder="请选择"-->
        <!--dictCode="xc_yes_no"/>-->
        <!--</a-form-item>-->
        <a-input style="display: none" v-decorator="['isApplyed', {}]"/>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="备注">
          <a-textarea rows="5" placeholder="请输入备注" v-decorator="['remark', {}]"/>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { httpAction } from '@/api/manage'
  import JDate from '@/components/jeecg/JDate'
  import JDictSelectTag from '@/components/dict/JDictSelectTag'
  import pick from 'lodash.pick'
  import moment from 'moment'
  import ATextarea from 'ant-design-vue/es/input/TextArea'

  export default {
    name: 'CustomerModal',
    components: {
      ATextarea,
      JDate,
      JDictSelectTag
    },
    data() {
      return {
        title: '操作',
        disableSubmit:true,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules: {},
        url: {
          add: '/customer/customer/add',
          edit: '/customer/customer/edit',
          customerAttentionList: '/customer/customer/queryCustomerAttentionByMainId'
        }
      }
    },
    methods: {
      add() {
        this.edit({
          isApplyed: '2',
          isAdd: true
        })

      },
      edit(record) {
        this.form.resetFields()
        this.model = Object.assign({}, record)
        //初始化明细表数据
        console.log(this.model.id)
        this.visible = true
        this.$nextTick(() => {

          // 如果是添加的默认  设置没有报名
          if (record.isAdd) {
            this.form.setFieldsValue({ isApplyed: record.isApplyed })
          } else {
            this.form.setFieldsValue(pick(this.model, 'name', 'sex', 'phone', 'wechat',
              'edu', 'likeLevel', 'isApplyed', 'remark','customerFrom','teacherName'))
          }


          // 时间格式化
        })
      },
      close() {
        this.$emit('close')
        this.visible = false
      },
      handleOk() {
        const that = this
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true
            let httpurl = ''
            let method = ''
            if (!this.model.id) {
              httpurl += this.url.add
              method = 'post'
            } else {
              httpurl += this.url.edit
              method = 'put'
            }
            let formData = Object.assign(this.model, values)
            //时间格式化
            httpAction(httpurl, formData, method).then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            }).finally(() => {
              that.confirmLoading = false
              that.close()
            })
          }
        })
      },
      handleCancel() {
        this.close()
      }
    }
  }
</script>

<style scoped>
  .ant-btn {
    padding: 0 10px;
    margin-left: 3px;
  }

  .ant-form-item-control {
    line-height: 0px;
  }

  /** 主表单行间距 */
  .ant-form .ant-form-item {
    margin-bottom: 10px;
  }

  /** Tab页面行间距 */
  .ant-tabs-content .ant-form-item {
    margin-bottom: 0px;
  }
</style>